<?php
/**
 * WHQ message controller implementation
 * 
 * @author Milan Rukavina <rukavinamilan@gmail.com>
 * @version 0.1
 * @package whq
 */

class WHQ_Controller_Message extends WHQ_Controller {

    private $lastError = '';
	
	public function post(&$data,&$headers){
        $subscriptions = WHQ_Model_Db::getInstance()->getSubscriptions($this->queueName);
        /*
        echo "headers: "; print_r($headers); echo "\n\n";
        echo "queue: " . $this->queueName . "\n\n";
        echo "url parts: "; print_r($this->requestParts); echo "\n\n";
        echo "body:" . $data . "\n\n";
        echo "subscriptions: "; print_r($subscriptions); echo "\n\n";
        */
        $responses = array();
        foreach ($subscriptions as $subscription){
            $currHeaders = $headers;
            $body = $this->sendMessage($subscription['uri'], $data, $currHeaders);
            $responses[] = array(
                'body'          => trim($body),
                'http_code'     => $currHeaders['http_code'],
                'content_type'  => $currHeaders['content_type'],
                'error'         => $this->lastError
            );
        }

        $this->processResponse($responses);
	}

    private function sendMessage($url,&$data,&$headers){
        $curlCon = curl_init($url);
        curl_setopt($curlCon,CURLOPT_HEADER,false);
        curl_setopt($curlCon,CURLOPT_NOBODY,false);
        //curl_setopt($curlCon, CURLOPT_CUSTOMREQUEST,'POST');
        //curl_setopt($curlCon, CURLINFO_HEADER_OUT, true);
        curl_setopt($curlCon, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($curlCon, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($curlCon, CURLOPT_POSTFIELDS, $data);
        //copy header content type from request
        $sendHeaders = array("User-Agent: WHQ");
        if(isset ($headers['Content-type'])){
            $sendHeaders[] = "Content-type: " . $headers['Content-type'];
        }
        curl_setopt($curlCon,CURLOPT_HTTPHEADER,$sendHeaders);
        curl_setopt($curlCon, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($curlCon, CURLOPT_FAILONERROR, TRUE);
        
        $body = curl_exec($curlCon);

        if(curl_errno($curlCon)){
            $this->lastError = curl_error($curlCon);
            curl_close($curlCon);
            return false;
        }
        else{
            $headers = curl_getinfo($curlCon);
            curl_close($curlCon);
            return $body;
        }

    }

    private function processResponse(&$responses){
        if(count($responses) == 0){
            WHQ_Router::headerStatus(WHQ_Router::STATUS_NOT_FOUND);
            return false;
        }
        else{
            WHQ_Router::headerStatus(WHQ_Router::STATUS_OK);
            header('Content-type: text/whq-response');
            echo '<?xml version="1.0"?>';
            echo '<whqresponse version="1.0">';
            echo '<moduleresponses>';
            foreach ($responses as $response){
                echo '<response httpcode="' . htmlspecialchars($response['http_code']) . '" contenttype="' . htmlspecialchars($response['content_type']) . '" error="' . htmlspecialchars($response['error']) . '" >';
                echo htmlspecialchars($response['body']);
                echo '</response>';
            }
            echo '</moduleresponses>';
            echo '</whqresponse>';
        }
    }

}
	
?>